package cn.neusoft.controller;

import cn.neusoft.pojo.Order;
import cn.neusoft.query.OrderQuery;
import cn.neusoft.service.IOrderService;
import cn.neusoft.vo.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    IOrderService orderService;

    @PostMapping      //  请求方式 + 请求路径   post  /order
    public ResultBean add(@RequestBody Order order){
        System.out.println("----OrderController-----add--------");
        System.out.println(order);
        return orderService.add(order);
    }

    @PutMapping    //   put   /order
    public ResultBean update(@RequestBody Order order){
        System.out.println("-------OrderController--------update-------------");
        System.out.println(order);
        return orderService.update(order);
    }

    //  delete   /order/1003
    @DeleteMapping("/{id}")   // http://localhost:8080/order/1003
    public ResultBean del(@PathVariable("id") Integer id){
        System.out.println("-------OrderController--------del-------------");
        System.out.println(id);
        return orderService.delete(id);
    }

    //  get   /order/1004
    @GetMapping("/{id}")   // http://localhost:8080/order/1003
    public ResultBean getById(@PathVariable("id") Integer id){
        System.out.println("-------OrderController--------getById-------------");
        System.out.println(id);
        return orderService.getById(id);
    }

    @GetMapping("/all")    //  get  /order/all
    public ResultBean getAll(){
        System.out.println("-------OrderController-----getAll--------");
        return orderService.getAll();
    }

    @PostMapping("/page")   // post  /order/page
    public ResultBean getByPage(@RequestBody OrderQuery orderQuery){
        System.out.println("-------OrderController--------getByPage-------------");
        System.out.println(orderQuery);
        return orderService.getByPage(orderQuery);
    }

    @GetMapping("/detail/{orderId}")   //get + /order/detail/1
    public ResultBean getByOrderId(@PathVariable("orderId") Integer orderId){
        System.out.println("-----OrderController------getByOrderId------");
        System.out.println(orderId);
        return orderService.getDetailsByOrderId(orderId);
    }


    @PostMapping("/batch/{ids}")   // post + /order/batch
    public ResultBean batchDel(@PathVariable("ids") String ids){
        System.out.println("----------BusinessController---------batchDel---------------");
        System.out.println(ids); //接收到的参数格式 ： 21,19
        String[] strIds = ids.split(",");  //字符串拆分  [21,19]
        System.out.println(Arrays.toString(strIds));
        List<Integer> numids = new ArrayList<>();  //创建一个Integer集合  service层需要的参数类型
        for (String strid: strIds) {
            numids.add(Integer.parseInt(strid));//将数组中的每一项转型为Integer再加入到集合中
        }
        System.out.println(numids);
        return orderService.batchDel(numids);
    }

}
